﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System.Drawing;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using XCI.Helper;

namespace XCI.Core
{
    /// <summary>
    /// 主题设置
    /// </summary>
    public class ThemeConfig
    {
        private static readonly JsonFile<ThemeConfig> JsonFile = new JsonFile<ThemeConfig>(IOHelper.GetRootFilePath("theme.json"));

        /// <summary>
        /// 样式
        /// </summary>
        public string Style { get; set; } = "Skin";

        /// <summary>
        /// 主题
        /// </summary>
        public string Skin { get; set; } = "DevExpress Style";

        /// <summary>
        /// 配色
        /// </summary>
        public string SvgPalette { get; set; } = "Default";

        /// <summary>
        /// 用户界面模式
        /// </summary>
        public string UserInterfaceMode { get; set; } = "DefaultUI";

        /// <summary>
        /// 自定义字体
        /// </summary>
        public string CustomFont { get; set; } = "微软雅黑, 11.25pt";

        /// <summary>
        /// 是否使用DirectX加速
        /// </summary>
        public bool UseDirectX { get; set; } = true;

        /// <summary>
        /// 是否使用高分屏支持
        /// </summary>
        public bool UseDpiAware { get; set; } = false;

        /// <summary>
        /// 获取配置
        /// </summary>
        public static ThemeConfig Get()
        {
            return JsonFile.Data;
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        public static void Save()
        {
            JsonFile.Save();
        }

        /// <summary>
        /// 设置系统主题字体
        /// </summary>
        public void InitAppTheme()
        {
            BonusSkins.Register();

            if (UseDirectX)
            {
                WindowsFormsSettings.ForceDirectXPaint();
            }

            if (UseDpiAware)
            {
                WindowsFormsSettings.SetPerMonitorDpiAware();
            }

            if (!string.IsNullOrEmpty(CustomFont))
            {
                WindowsFormsSettings.FontBehavior = WindowsFormsFontBehavior.Default;
                var fontObj = (Font)ObjectHelper.GetObjectFromString(CustomFont, typeof(Font));
                WindowsFormsSettings.DefaultFont = fontObj;
                WindowsFormsSettings.DefaultMenuFont = fontObj;
                WindowsFormsSettings.DefaultPrintFont = fontObj;
            }

            if (!string.IsNullOrEmpty(UserInterfaceMode))
            {
                switch (UserInterfaceMode)
                {
                    case "DefaultUI":
                        WindowsFormsSettings.CompactUIMode = DefaultBoolean.False;
                        WindowsFormsSettings.TouchUIMode = TouchUIMode.False;
                        break;
                    case "CompactUI":
                        WindowsFormsSettings.CompactUIMode = DefaultBoolean.True;
                        WindowsFormsSettings.TouchUIMode = TouchUIMode.False;
                        break;
                    case "TouchUI":
                        WindowsFormsSettings.CompactUIMode = DefaultBoolean.False;
                        WindowsFormsSettings.TouchUIMode = TouchUIMode.True;
                        break;
                }
            }

            switch (Style)
            {
                case "Skin":
                    WindowsFormsSettings.EnableFormSkins();
                    UserLookAndFeel.Default.SetStyle(LookAndFeelStyle.Skin, false, false, Skin, SvgPalette);
                    break;
                case "Native":
                    UserLookAndFeel.Default.SetWindowsXPStyle();
                    break;
                case "Style3D":
                    UserLookAndFeel.Default.SetStyle3D();
                    break;
                case "Office2003":
                    UserLookAndFeel.Default.SetOffice2003Style();
                    break;
                case "Flat":
                    UserLookAndFeel.Default.SetFlatStyle();
                    break;
                case "UltraFlat":
                    UserLookAndFeel.Default.SetUltraFlatStyle();
                    break;
            }
        }
    }
}